// pages/add/add.js

var utils = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index:1,
    texts:'',
    nums:[1,2,3,4,5],
    places:['河北师范大学','河北科技大学','河北医科大学','不限'],
    objectNum:[
      {id:0,name:1},
      {id:1,name:2},
      {id:2,name:3},
      {id:3,name:4},
      {id:4,name:5}
    ],
    objectPlace:[
      {id:0,name:'河北师范大学'},
      {id:1,name:'河北科技大学'},
      {id:2,name:'河北医科大学'},
      {id:3,name:'不限'}
    ],
    indexP:0,
    indexN:0,
    colleges:['软件学院','计算机学院','数学科学学院','美术学院','资源环境学院','不限'],
    onjectCollege:[
      {id:0,name:'软件学院'},
      {id:1,name:'计算机学院'},
      {id:2,name:'数学科学学院'},
      {id:3,name:'美术学院'},
      {id:4,name:'资源环境学院'},
      {id:5,name:'不限'}
    ],
    indexC:0,
    ages:['大一','大二','大三','大四','研一','研二','研三','不限'],
    objectAge:[
      {id:0,name:'大一'},
      {id:1,name:'大二'},
      {id:2,name:'大三'},
      {id:3,name:'大四'},
      {id:4,name:'研一'},
      {id:5,name:'研二'},
      {id:6,name:'研三'},
      {id:7,name:'不限'}
    ],
    indexA:0,
    sexs:['男','女','不限'],
    objectSex:[
      {id:1,name:'男'},
      {id:2,name:'女'},
      {id:3,name:'不限'}
    ],
    indexS:1,
    date:'2021-01-01',
    time:'00:00'

  },

  // 获取帖子内容
  areaIn:function(e){
    var that = this;
    that.setData({
      texts:e.detail.value
    })
  },


  // 地区修改
  bindPlaceChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      indexP: e.detail.value
    })
  },

  // 专业修改
  bindCollegeChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      indexC: e.detail.value
    })
  },


  //人数修改
  bindNumChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      indexN: e.detail.value
    })
  },

  // 年级修改
  bindAgeChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      indexA: e.detail.value
    })
  },

    // 性别修改
    bindSexChange: function(e) {
      console.log('picker发送选择改变，携带值为', e.detail.value)
      this.setData({
        indexS: e.detail.value
      })
    },


    //日期修改
    bindDateChange: function(e) {
      console.log('picker发送选择改变，携带值为', e.detail.value)
      this.setData({
        date: e.detail.value
      })
    },


    // 时间修改
    bindTimeChange: function(e) {
      console.log('picker发送选择改变，携带值为', e.detail.value)
      this.setData({
        time: e.detail.value
      })
    },


  // 按钮发帖
  addItem:function(){
    //判断是否登录
    var user_id=wx.getStorageSync('user_id');
    console.log(user_id)
    if(user_id==''){
      wx.showModal({
        title:'温馨提示',
        content:'需要先登录',
        confirmText:'登录',
        cancelText:'取消',
        success:res=>{
          if(res.confirm){
            wx.switchTab({
              url: '/pages/person/person',
            })
          }
        }
      })
    }else{
      var date=new Date()
      let {indexP,indexN,indexS,indexA,indexC}=this.data
      var obj={
        //post_id:post21112517300001,
        post_nameId:wx.getStorageSync('user_id'),
        post_content:this.data.texts,
        post_time:date.toLocaleString(),
        post_now_num:0,
        post_deadline:''+this.data.date+' '+this.data.time,
        post_require_num:this.data.nums[indexN],
        post_restrict_university:this.data.places[indexP]=='不限'?'':this.data.places[indexP],
        post_restrict_major:this.data.colleges[indexC]=='不限'?'':this.data.colleges[indexC],
        post_restrict_grade:this.data.ages[indexA]=='不限'?'':this.data.ages[indexA],
        post_restrict_sex:parseInt(this.data.indexS)+1,
        post_share_num:0,
        post_report_num:0,
        post_ifrevoke:'否',
        post_ifdelete:'否',
        post_num_id:'',
        post_topic:'',
        post_restrict_remark:''
      };
      wx.request({
        url: 'https://api.yidan.work:2004/post',
        header:{'Access-Control-Allow-Origin':'*'},
        header:{'Access-Control-Allow-Methods':'GET,POST'},
        data:obj,
        method: 'POST',
        success:(res)=>{
          console.log(res.data);
          wx.switchTab({
            url: '/pages/home/home',
          })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    

  },

  

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     this.setData({
      indexA:0,
      indexC:0,
      indexN:0,
      indexP:0,
      indexS:0,
      texts:'',
      date:'2021-01-01',
      time:'00:00'
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})